using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Xml;
using System.Collections.Specialized;
using CodePlex.Resourceful.Wadl;
using CodePlex.Resourceful.Bookmarks.Controllers;
using CodePlex.Resourceful.Bookmarks.Model;

namespace CodePlex.Resourceful.Bookmarks
{
    public class BookmarkService : IService
    {

        private readonly BookmarkEngine _engine;
        private readonly ApplicationDescription _app;
        private readonly UsersController _usersController;
        private readonly BookmarksController _bookmarksController;
       
        public BookmarkService()
        {

            _engine = new BookmarkEngine();

            _usersController = new UsersController(_engine);
            _bookmarksController = new BookmarksController(_engine);


            LocalApplicationDescription app = new LocalApplicationDescription();


            // get-user
            LocalApplicationMethod getUser = app.NewMethod("getUser", HttpMethod.Get, _usersController.GetUser);
            getUser.NewResponseRepresentation(MediaType.ApplicationXml);
            getUser.NewResponseRepresentation(MediaType.ApplicationExWwwFormUrlencoded);

            ApplicationResource userResource = app.NewResource("users/{username}", new TemplateParameter("username", "xsd:string"));
            app.Bind(userResource, getUser);


            // create-user
            LocalApplicationMethod createUser = app.NewMethod("createUser", HttpMethod.Post, _usersController.CreateUser);
            createUser.NewRequestRepresentation(MediaType.ApplicationXml);

            ApplicationResource createUserResource = app.Resources["users"];
            app.Bind(createUserResource, createUser);


            // update-user
            LocalApplicationMethod updateUser = app.NewMethod("updateUser", HttpMethod.Put, _usersController.UpdateUser);
            updateUser.NewRequestRepresentation(MediaType.ApplicationXml);

            app.Bind(userResource, updateUser);


            // delete-user
            LocalApplicationMethod deleteUser = app.NewMethod("deleteUser", HttpMethod.Delete, _usersController.DeleteUser);

            app.Bind(userResource, deleteUser);



            // create-bookmark
            LocalApplicationMethod createBookmark = app.NewMethod("createBookmark", HttpMethod.Post, _bookmarksController.CreateBookmark);
            createBookmark.NewRequestRepresentation(MediaType.ApplicationXml);

            ApplicationResource bookmarksResource = app.NewResource("users/{username}/bookmarks", new TemplateParameter("username", "xsd:string"));
            app.Bind(bookmarksResource, createBookmark);


            // delete-bookmark
            LocalApplicationMethod deleteBookmark = app.NewMethod("deleteBookmark", HttpMethod.Delete, _bookmarksController.DeleteBookmark);

            ApplicationResource bookmarkResource = app.NewResource("users/{username}/bookmarks/{uri-hash}", 
                new TemplateParameter("username", "xsd:string"),
                new TemplateParameter("uri-hash", "xsd:string")
                );

            app.Bind(bookmarkResource, deleteBookmark);


            // update-bookmark
            LocalApplicationMethod updateBookmark = app.NewMethod("updateBookmark", HttpMethod.Put, _bookmarksController.UpdateBookmark);

            app.Bind(bookmarkResource, updateBookmark);

            // get-bookmark
            LocalApplicationMethod getBookmark = app.NewMethod("getBookmark", HttpMethod.Get, _bookmarksController.GetBookmark);
            getBookmark.NewResponseRepresentation(MediaType.ApplicationXml);
            app.Bind(bookmarkResource, getBookmark);



            // get-bookmarks
            LocalApplicationMethod getBookmarks = app.NewMethod("getBookmarks", HttpMethod.Get, _bookmarksController.GetBookmarks);
            getBookmarks.NewResponseRepresentation(MediaType.ApplicationXml);
            app.Bind(bookmarksResource, getBookmarks);

            // get-bookmarks-atom
            LocalApplicationMethod getBookmarksAtom = app.NewMethod("getBookmarksAtom", HttpMethod.Get, _bookmarksController.GetBookmarksAtom);
            getBookmarksAtom.NewResponseRepresentation(MediaType.ApplicationAtomXml);
            ApplicationResource bookmarksAtomResource = app.NewResource("users/{username}/bookmarks.atom", new TemplateParameter("username", "xsd:string"));
            app.Bind(bookmarksAtomResource, getBookmarksAtom);
         

            _app = app;

        }


      



        public void Process(IHttpContext context)
        {
           
            ApplicationDescriptionService ads = new ApplicationDescriptionService(_app,"app.wadl");

            ads.Process(context);

            if (!context.Response.IsEnded)
            {
                // take over default handler
                _usersController.RenderStatus(ads.NewContext(context), HttpStatus.NotFound);
            }

        }

     
    }
}
